<?php

class product extends CI_Controller
{
    protected $_table = "mock_products";

    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('product_model');
        $this->load->model('brand_model');
        $this->load->model('category_model');
        $this->load->helper(array('form', 'url'));
        $this->load->helper("url");
        $this->load->library('upload');
    }


    public function index()
    {
        $data['title'] = "Update Product";
        $data['path'] = "product/update";
    }

    public function addProduct()
    {
        $data['title'] = "Insert Product";
        $data['path'] = "product/add";
        $data['listbrand'] = $this->brand_model->listBran_product();
        $data['listcate'] = $this->category_model->listCate_product();
        $data['errors'] = "";
        if ($this->input->post('submit') != NULL) {
           $this->form_validation->set_rules('name', 'Product Name', 'required');
           $this->form_validation->set_rules('price', 'Price', 'required');
            if ($this->form_validation->run() == FALSE) {
                $this->load->view("index", $data);
            } else {


                foreach ($_FILES['images']["name"] as $key => $value) {

                    $_FILES['picture']['name'] = $_FILES['images']['name'][$key];
                    $_FILES['picture']['type'] = $_FILES['images']['type'][$key];
                    $_FILES['picture']['tmp_name'] = $_FILES['images']['tmp_name'][$key];
                    $_FILES['picture']['error'] = $_FILES['images']['error'][$key];
                    $_FILES['picture']['size'] = $_FILES['images']['size'][$key];
                    //config upload images
                    $config['upload_path'] = './template/home/image';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '1000';
                    $config['max_width'] = '1024';
                    $config['max_height'] = '768';
                    $config['max_filename'] = '100';

                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);
                    if (!$this->upload->do_upload('picture')) {
                        $data['error'] = array('error' => $this->upload->display_errors());

                        $this->load->view('index', $data);
                    } else {
                        $data = $this->upload->data();
                        $name_array[] = $data['file_name'];
                    }
                }
                $name_image = implode(',', $name_array);
                $sale = $this->input->post("sale");
                $name = $this->input->post("name");
                $price = $this->input->post("price");
                $description = $this->input->post("description");
                $category = $this->input->post("category");
                $brand = $this->input->post("brand");
                $new = $this->input->post("new");

                $dataarray = array(
                    'product_sale' => $sale,
                    'product_name' => $name,
                    'product_price' => $price,
                    'product_image' => $name_image,
                    'product_description' => $description,
                    'cate_id' => $category,
                    'brand_id' => $brand,
                    'product_new' => $new
                );
                $this->product_model->addProduct($dataarray);
                redirect(base_url() . "admin/product/addProduct");
                exit();
            }

        } else {
            $this->load->view("index", $data);
        }
    }


    public function updateProduct()
    {
        $id = $this->uri->segment(4);
        $data['title'] = "Edit Product";
        $data['path'] = "product/update";
        $data['errors'] = "";
        $data['info'] = $this->product_model->getdata($id);
        $this->load->view('index', $data);
        if ($this->input->post('submit') != NULL) {
            $this->form_validation->set_rules('name', 'ProductName', 'required');
            $this->form_validation->set_rules('price', 'ProductPrice', 'required');

            if ($this->form_validation->run() == NULL) {
                $this->load->view('index', $data);
            } else {
                $sale = $this->input->post("sale");
                $name = $this->input->post("name");
                $price = $this->input->post("price");
                $image = $this->input->post("image");
                $description = $this->input->post("description");
                $listcate = $this->input->post("listcate");
                $bran = $this->input->post("bran");
                $new = $this->input->post("new");
                $hot = $this->input->post("hot");
                $dbarray = array(
                    "product_sale" => $sale,
                    "product_name" => $name,
                    "product_price" => $price,
                    "product_description" => $description,
                    "product_image" => $image,
                    "list_cate_id" => $listcate,
                    "bran_id" => $bran,
                    "product_new" => $new,
                    "product_hot" => $hot
                );
                $checkProductname = $this->product_model->checkProduct("product_name", $name, $id);
                $checkImage = $this->product_model->checkProduct("product_image", $image, $id);
                if ($checkProductname == FALSE) {
                    $data['errors'] = "Product đã tồn tại";
                    $this->load->view('index', $data);
                } elseif ($checkImage == FALSE) {
                    $data['errors'] = "Vui lòng chọn ảnh khác";
                    $this->load->view('index', $data);
                } else {
                    $this->product_model->update($id, $dbarray);
                    redirect(base_url() . "default.php/admin/product");
                    exit();
                }
            }
        }
    }

}